package com.ilarele.shopping.client.util;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.ilarele.shopping.Product;
import com.ilarele.shopping.client.database.ShoppingDB;

public class DBUtil {
	public static final String PRODUCT_ID = "_id";
	public static final String NAME = "name";

	public static final String LOCATION = "location";
	public static final String PRICE = "price";
	public static final String RATING = "rating";
	public static final String TAGS = "tags";

	public static void insertNewProduct(Context ctx, Product prod) {
		ContentResolver cr = ctx.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(ShoppingDB.PRODUCT_ID, prod.idProd);
		values.put(ShoppingDB.PICTURE, prod.pPicture.imageUrl);
		values.put(ShoppingDB.NAME, prod.pName.name);
		values.put(ShoppingDB.LOCATION_NAME, prod.pInfo.pLocation.name);
		values.put(ShoppingDB.LOCATION_LAT, prod.pInfo.pLocation.latitude);
		values.put(ShoppingDB.LOCATION_LONG, prod.pInfo.pLocation.longitude);
		values.put(ShoppingDB.PRICE, prod.pInfo.price);
		values.put(ShoppingDB.RATING, prod.pInfo.rating);
		values.put(ShoppingDB.TAGS, prod.pTags.tags);
		cr.insert(ShoppingDB.CONTENT_URI_PROD, values);
	}

	public static ArrayList<Product> getMyProducts(Activity activity) {
		ArrayList<Product> result = new ArrayList<Product>();
		Cursor cursor = activity.managedQuery(ShoppingDB.CONTENT_URI_PROD,
				null, null, null, null);
		int iId = cursor.getColumnIndex(ShoppingDB.PRODUCT_ID);
		int iPicture = cursor.getColumnIndex(ShoppingDB.PICTURE);
		int iName = cursor.getColumnIndex(ShoppingDB.NAME);
		int iLocation = cursor.getColumnIndex(ShoppingDB.LOCATION_NAME);
		int iLat = cursor.getColumnIndex(ShoppingDB.LOCATION_LAT);
		int iLong = cursor.getColumnIndex(ShoppingDB.LOCATION_LONG);
		int iPrice = cursor.getColumnIndex(ShoppingDB.PRICE);
		int iRating = cursor.getColumnIndex(ShoppingDB.RATING);
		int iTags = cursor.getColumnIndex(ShoppingDB.TAGS);

		while (cursor.moveToNext()) {
			Product prod = new Product();
			prod.idProd = cursor.getString(iId);
			prod.pPicture.imageUrl = cursor.getString(iPicture);
			prod.pName.name = cursor.getString(iName);
			prod.pInfo.pLocation.name = cursor.getString(iLocation);
			prod.pInfo.pLocation.latitude = cursor.getDouble(iLat);
			prod.pInfo.pLocation.longitude = cursor.getDouble(iLong);
			prod.pInfo.price = cursor.getFloat(iPrice);
			prod.pInfo.rating = cursor.getInt(iRating);
			prod.pTags.tags = cursor.getString(iTags);
			result.add(prod);
		}
		cursor.close();
		return result;
	}

	public static void updateProduct(Context ctx, Product prod) {
		ContentResolver cr = ctx.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(ShoppingDB.PICTURE, prod.pPicture.imageUrl);
		values.put(ShoppingDB.NAME, prod.pName.name);
		values.put(ShoppingDB.LOCATION_NAME, prod.pInfo.pLocation.name);
		values.put(ShoppingDB.LOCATION_LAT, prod.pInfo.pLocation.latitude);
		values.put(ShoppingDB.LOCATION_LONG, prod.pInfo.pLocation.longitude);
		values.put(ShoppingDB.PRICE, prod.pInfo.price);
		values.put(ShoppingDB.RATING, prod.pInfo.rating);
		values.put(ShoppingDB.TAGS, prod.pTags.tags);
		cr.update(ShoppingDB.CONTENT_URI_PROD, values, ShoppingDB.PRODUCT_ID
				+ "=" + prod.idProd, null);
	}

	public static void clearDB(Context ctx) {
		Global.setDbCleared();
		ContentResolver cr = ctx.getContentResolver();
		cr.delete(ShoppingDB.CONTENT_URI_PROD, null, null);
	}

	public static void updatePictureRate(Activity activity, Product product) {
		ContentResolver cr = activity.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(ShoppingDB.RATED_PICTURES_VALUE,
				product.pPicture.unsavedRate);

		// check if exists
		Cursor cursor = activity.managedQuery(
				ShoppingDB.CONTENT_URI_RATED_PICTURES, null,
				ShoppingDB.RATED_PICTURES_ID + "=" + product.pPicture.id, null,
				null);

		if (cursor.moveToNext()) {
			cr.update(ShoppingDB.CONTENT_URI_RATED_PICTURES, values,
					ShoppingDB.RATED_PICTURES_ID + "=" + product.pPicture.id,
					null);
		} else {
			values.put(ShoppingDB.RATED_PICTURES_ID, product.pPicture.id);
			cr.insert(ShoppingDB.CONTENT_URI_RATED_PICTURES, values);
		}
		cursor.close();
	}

	public static void updateNameRate(Activity activity, Product product) {
		ContentResolver cr = activity.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(ShoppingDB.RATED_NAMES_VALUE, product.pName.unsavedRate);

		// check if exists
		Cursor cursor = activity.managedQuery(
				ShoppingDB.CONTENT_URI_RATED_NAMES, null,
				ShoppingDB.RATED_NAMES_ID + "=" + product.pName.id, null, null);

		if (cursor.moveToNext()) {
			cr.update(ShoppingDB.CONTENT_URI_RATED_NAMES, values,
					ShoppingDB.RATED_NAMES_ID + "=" + product.pName.id, null);
		} else {
			values.put(ShoppingDB.RATED_NAMES_ID, product.pName.id);
			cr.insert(ShoppingDB.CONTENT_URI_RATED_NAMES, values);
		}
		cursor.close();
	}

	public static void updateTagsRate(Activity activity, Product product) {
		ContentResolver cr = activity.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(ShoppingDB.RATED_TAGS_VALUE, product.pTags.unsavedRate);

		// check if exists
		Cursor cursor = activity.managedQuery(
				ShoppingDB.CONTENT_URI_RATED_TAGS, null,
				ShoppingDB.RATED_TAGS_ID + "=" + product.pTags.id, null, null);

		if (cursor.moveToNext()) {
			cr.update(ShoppingDB.CONTENT_URI_RATED_TAGS, values,
					ShoppingDB.RATED_TAGS_ID + "=" + product.pTags.id, null);
		} else {
			values.put(ShoppingDB.RATED_TAGS_ID, product.pTags.id);
			cr.insert(ShoppingDB.CONTENT_URI_RATED_TAGS, values);
		}
		cursor.close();
	}

	public static void updateInfoRate(Activity activity, Product product) {
		ContentResolver cr = activity.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(ShoppingDB.RATED_INFO_VALUE, product.pInfo.unsavedRate);

		// check if exists
		Cursor cursor = activity.managedQuery(
				ShoppingDB.CONTENT_URI_RATED_INFO, null,
				ShoppingDB.RATED_INFO_ID + "=" + product.pInfo.id, null, null);

		if (cursor.moveToNext()) {
			cr.update(ShoppingDB.CONTENT_URI_RATED_INFO, values,
					ShoppingDB.RATED_INFO_ID + "=" + product.pInfo.id, null);
		} else {
			values.put(ShoppingDB.RATED_INFO_ID, product.pInfo.id);
			cr.insert(ShoppingDB.CONTENT_URI_RATED_INFO, values);
		}
		cursor.close();
	}

	public static void fillRate(Activity activity, Product simpleProd) {
		fillInfoRate(activity, simpleProd);
		fillNameRate(activity, simpleProd);
	}

	public static void fillPictureRate(Activity activity, Product simpleProd) {
		Cursor cursor = activity.managedQuery(
				ShoppingDB.CONTENT_URI_RATED_PICTURES, null,
				ShoppingDB.RATED_PICTURES_ID + "=" + simpleProd.pPicture.id,
				null, null);

		if (cursor.moveToNext()) {
			int index = cursor.getColumnIndex(ShoppingDB.RATED_PICTURES_VALUE);
			simpleProd.pPicture.dbRate = simpleProd.pPicture.unsavedRate = cursor
					.getInt(index);
		} else
			simpleProd.pPicture.dbRate = simpleProd.pPicture.unsavedRate = 0;
		cursor.close();
	}

	public static void fillNameRate(Activity activity, Product simpleProd) {
		Cursor cursor = activity.managedQuery(
				ShoppingDB.CONTENT_URI_RATED_NAMES, null,
				ShoppingDB.RATED_NAMES_ID + "=" + simpleProd.pName.id, null,
				null);

		if (cursor.moveToNext()) {
			int index = cursor.getColumnIndex(ShoppingDB.RATED_NAMES_VALUE);
			simpleProd.pName.dbRate = simpleProd.pName.unsavedRate = cursor
					.getInt(index);
		} else
			simpleProd.pName.dbRate = simpleProd.pName.unsavedRate = 0;
		cursor.close();
	}

	public static void fillInfoRate(Activity activity, Product simpleProd) {
		Cursor cursor = activity.managedQuery(
				ShoppingDB.CONTENT_URI_RATED_INFO, null,
				ShoppingDB.RATED_INFO_ID + "=" + simpleProd.pInfo.id, null,
				null);

		if (cursor.moveToNext()) {
			int index = cursor.getColumnIndex(ShoppingDB.RATED_INFO_VALUE);
			simpleProd.pInfo.dbRate = simpleProd.pInfo.unsavedRate = cursor
					.getInt(index);
		} else
			simpleProd.pInfo.dbRate = simpleProd.pInfo.unsavedRate = 0;
		cursor.close();
	}

	public static void fillNameTags(Activity activity, Product simpleProd) {
		Cursor cursor = activity.managedQuery(
				ShoppingDB.CONTENT_URI_RATED_TAGS, null,
				ShoppingDB.RATED_TAGS_ID + "=" + simpleProd.pTags.id, null,
				null);

		if (cursor.moveToNext()) {
			int index = cursor.getColumnIndex(ShoppingDB.RATED_TAGS_VALUE);
			simpleProd.pTags.dbRate = simpleProd.pTags.unsavedRate = cursor
					.getInt(index);
		} else
			simpleProd.pTags.dbRate = simpleProd.pTags.unsavedRate = 0;
		cursor.close();
	}
}
